<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/11/12
 * Time: 17:07
 */

namespace app\adminapiv3\service;


use app\common\service\IpsType;

class IpsRule extends BaseService
{
    /**
     * @name 列表
     * @param $host_id
     * @param $params
     * @return array
     * @author wx
     * @date 2019/11/13 10:40
     */
    public function list($host_id, $params)
    {
        $server = new \app\common\service\IpsRule();
        $list = $server->list($host_id, $params);

        return $list;
    }

    /**
     * @name 列表
     * @param $id
     * @param $updateData
     * @return bool
     * @author wx
     * @date 2019/11/13 10:43
     */
    public function modify($host_id, $id, $updateData)
    {
        $server = new \app\common\service\IpsRule();
        $server->modify(['host_id' => $host_id, 'id' => $id], $updateData);

        return true;
    }

    /**
     * @name 删除
     * @param $id
     * @return bool
     * @author wx
     * @date 2019/11/13 10:50
     */
    public function del($host_id, $id)
    {
        $server = new \app\common\service\IpsRule();
        $condition[] = array('id', 'in', $id);
        $server->del($condition);

        return true;
    }

    /**
     * @name 清空
     * @param $clear
     * @return bool
     * @author wx
     * @date time
     */
    public function clear($host_id, $clear)
    {
        if ($clear != 1) {
            $this->setError($this->CODE_FAIL, '参数错误');
            return false;
        }

        $server = new \app\common\service\IpsRule();
        $condition['host_id'] = $host_id;
        $server->del($condition);

        return true;
    }

    /**
     * @name 添加
     * @param $addData
     * @author wx
     * @date 2019/11/13 16:48
     */
    public function add($addData)
    {
        $typeServer = new IpsType();

        //1.验证限制类型
        $typeArr = $typeServer->findData(['id' => $addData['type'], 'type' => 'freeRule']);
        if(empty($typeArr)){
            $this->setError($this->CODE_FAIL, '类型错误!');
            return false;
        }

        $addData['ctime'] = time();
        $addData['execute_content'] = '';

        //2. 添加自定义规则
        $server = new \app\common\service\IpsRule();
        $insertId = $server->add($addData);

        $updateData = [
            'execute_content' => $this->encryptRule($insertId, $addData, $addData['type']),
        ];

        //修改
        $server->modify(['id' => $insertId], $updateData);

        $this->addExtraResponse($insertId, $addData);//添加额外的限制返回数据规则
        $this->addExtraRequestFile($insertId, $addData);//添加额外的限制请求文件规则
        return true;


    }

    /**
     * @name 加密规则
     * @param $data
     * @return mixed
     * @author wx
     * @date 2019/9/16 10:27
     */
    public function encryptRule($id, $data, $type)
    {
        $str = '';
        if($data['type'] == '9'){//端口
            $str =  c_mod_port($id, $data['name'], $data['type'], $data['content'], $data['intranet']);
        }elseif ($data['type'] == '10'){//域名
            $str =  c_mod_domain($id, $data['name'], $data['type'], $data['content'], $data['intranet']);
        }elseif ($data['type'] == '11'){//IP
            $str =  c_mod_ip($id, $data['name'], $data['type'], $data['content'], $data['intranet']);
        }elseif ($data['type'] == '12'){//请求类型限制
            $str =  c_mod_RequestType($id, $data['name'], $data['type'], $data['content'], $data['intranet']);
        }elseif ($data['type'] == '13'){//请求长度限制
            $str =  c_mod_RequestLen($id, $data['name'], $data['type'], $data['content'], $data['intranet']);
        }elseif ($data['type'] == '14'){//请求文件限制
            $str =  c_mod_RequestFile($id, $data['name'], $data['type'], $data['content'], $data['intranet']);
        }elseif ($data['type'] == '15'){//请求目录限制
            $str =  c_mod_RequestFolder($id, $data['name'], $data['type'], $data['content'], $data['intranet']);
        }elseif ($data['type'] == '16'){//请求参数限制
            $str =  c_mod_RequestParameter($id, $data['name'], $data['type'], $data['content'], $data['intranet']);
        }elseif ($data['type'] == '17'){
            $str =  c_mod_ResponseContent($id, $data['name'], $data['type'], $data['content'], $data['intranet']);
        }elseif ($data['type'] == 'user-request-file-limit_gbk'){
            $str =  c_mod_RequestGbkFile($id, $data['name'], $data['type'], $data['content'], $data['intranet']);
        }elseif ($data['type'] == 'user-response-content-limit_url'){
            $str =  c_mod_ResponseUrlContent($id, $data['name'], $data['type'], $data['content'], $data['intranet']);
        }elseif ($data['type'] == 'user-response-content-limit_gbk'){
            $str =  c_mod_ResponseGbkContent($id, $data['name'], $data['type'], $data['content'], $data['intranet']);
        }elseif ($data['type'] == 'user-response-content-limit_utf8'){
            $str =  c_mod_ResponseUtf8Content($id, $data['name'], $data['type'], $data['content'], $data['intranet']);
        }

        return $str;
    }

    /**
     * @name 处理额外用户响应内容生成规则
     * @param $fid
     * @param $data
     * @author wx
     * @date 2019/11/14 13:47
     */
    public function addExtraResponse($fid, $data)
    {
        if ($data['type'] == '17')
        {
            $server = new \app\common\service\IpsRule();
            
            $data['fid'] = $fid;

            //gbk
            $gbkInsertId = $server->add($data);
            $gbkUpdateData = [
                'execute_content' => $this->encryptRule($gbkInsertId, $data, 'user-response-content-limit_gbk'),
            ];
            $server->modify(['id' => $gbkInsertId], $gbkUpdateData);

            //url
            $urlInsertId = $server->add($data);
            $updateData = [
                'execute_content' => $this->encryptRule($urlInsertId, $data, 'user-response-content-limit_url'),
            ];
            $server->modify(['id' => $urlInsertId], $updateData);

            //utf-8
            $utf8InsertId = $server->add($data);
            $updateData = [
                'execute_content' => $this->encryptRule($utf8InsertId, $data, 'user-response-content-limit_utf8'),
            ];
            $server->modify(['id' => $utf8InsertId], $updateData);
        }
    }


    /**
     * @name 处理额外用户请求文件生成规则
     * @param $fid
     * @param $data
     * @author wx
     * @date 2019/11/14 13:48
     */
    public function addExtraRequestFile($fid, $data)
    {
        if ($data['type'] == '14')
        {
            $data['fid'] = $fid;
            $server = new \app\common\service\IpsRule();
            $gbkInsertId = $server->add($data);
            $updateData = [
                'execute_content' => $this->encryptRule($gbkInsertId, $data, 'user-request-file-limit_gbk'),
            ];
            $server->modify(['id' => $gbkInsertId], $updateData);
        }
    }

}